Page({

  /**
   * 页面的初始数据
   */
  data: {
    productDetail: {},
    productId: '1',
    openId: '',
    showModalStatus: false,
    buyNum: 1,
    imgUrl: '',
    kind: ''
  },

  /* 加 */
  plusNum: function() {
    let num = this.data.buyNum;
    num++;
    this.setData({
      buyNum: num
    })
  },

  /* 减 */
  deleteNum: function() {
    let num = this.data.buyNum;
    if (num > 1) {
      num--;
    } else {
      wx.showToast({
        title: '请至少选择一个商品',
        icon: 'none',
        duration: 2000
      })
    }
    this.setData({
      buyNum: num
    })
  },

  /* 显示遮罩层 */
  showBuyModal: function() {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      /**
       * http://cubic-bezier.com/ 
       * linear 动画一直较为均匀
       * ease 从匀速到加速在到匀速
       * ease-in 缓慢到匀速
       * ease-in-out 从缓慢到匀速再到缓慢
       * 
       * http://www.tuicool.com/articles/neqMVr
       * step-start 动画一开始就跳到 100% 直到动画持续时间结束 一闪而过
       * step-end 保持 0% 的样式直到动画持续时间结束 一闪而过
       */
      timingFunction: "ease",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(), // export 方法每次调用后会清掉之前的动画操作。
      showModalStatus: true
    })
    setTimeout(() => {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export() // export 方法每次调用后会清掉之前的动画操作。
      })
      console.log(this)
    }, 200)
  },

  /* 关闭遮罩层 */
  hideBuyModal: function() {
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "ease",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function() {
      animation.translateY(0).step();
      this.setData({
        animationData: animation.export(),
        showModalStatus: false
      })
    }.bind(this), 200)
  },

  /* 提交 */
  submit: function() {
    this.addCart();
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "ease",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function() {
      animation.translateY(0).step();
      this.setData({
        animationData: animation.export(),
        showModalStatus: false
      })
    }.bind(this), 200)
  },

  /* 添加购物车 */
  addCart: function() {
    var url = wx.getStorageSync("config")
    let that = this;
    let productId = that.data.productDetail.productSortId;
    let openId = that.data.userId;
    let num = that.data.buyNum;
    wx.request({
      url: url.service.homeUrl + "/huiguniang/shopcar/addProduct?openId=" + openId + "&productId=" + productId + "&productNumber=" + num,
      // url: "https://www.houmeiyuan.club/huiguniang/shopcar/addProduct?openId=orXxp5IJRbrsKSYzLpdNEUxEQL-M&productId=2&productNumber=4",
      method: 'POST',
      header: {
        'Content-Type': 'application/json'
      },
      success: function(res) {
        console.log(res)
        if (res.data.status.returnCode === 1) {
          wx.showToast({
            title: '添加成功',
            icon: 'success',
            duration: 2000
          })
        }
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  },

  /* 获取商品信息 */
  getProductsDetail: function() {
    var url = wx.getStorageSync("config")
    let that = this;
    let productId = that.data.kind;
    wx.request({
      url: url.service.homeUrl + "/huiguniang/goods/fetchByProductId?productId=" + productId,
      method: 'POST',
      header: {
        'Content-Type': 'application/json'
      },
      success: function(res) {
        console.log(res)
        if (res.data.status.returnCode === 1) {
          that.setData({
            productDetail: res.data.result,
            imgUrl: url.service.imgUrl
          })
        }
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  },

  /* 添加浏览历史 */
  addHistory() {
    var url = wx.getStorageSync("config")
    let that = this;
    let userId = that.data.userId;
    let id = that.data.id;
    wx.request({
      url: url.service.homeUrl + "/history/insert",
      data: {
        userId: userId,
        productId: id,
      },
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      kind: options.kind
    })
    let kind = options.kind;
    console.log(kind);

    //获取商品id
    console.log(options)
    let productId = options.productId;
    console.log(productId)
    this.setData({
      productId: productId
    })
    //获取用户id
    let userId = wx.getStorageSync('userId');
    this.setData({
      userId: userId
    })
    //获取商品列表
    this.getProductsDetail();
    //添加浏览历史
    // this.addHistory();
  },

  linkCart: function() {
    wx.reLaunch({
      url: '/pages/cart/cart'
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})